package com.alpaak.common.utils;

import org.apache.oro.text.regex.*;

/**
 * @ClassName: CommonVerify 
 * @Description: 公用验证类
 * @author: FengWenJu
 * @date: 2017年11月3日 上午11:26:27
 */
public class CommonVerify {
	//是否是数字
	public static boolean numberVerify(String name) {
		if (name == null)
			return false;
		boolean isNumberChar = true;
		int i = 0;

		while ((isNumberChar) && (i < name.length())) {
			char x = name.charAt(i);
			if ((x < '0') || (x > '9'))
				isNumberChar = false;
			i++;
		}
		return isNumberChar;
	}
	
	public static boolean charVerify(String name) {
		if (name == null) {
			return false;
		}
		char[] achar = name.toCharArray();
		for (int i = 0; i < achar.length; i++) {
			if ((achar[i] < 'A') || (achar[i] > 'z')) {
				return false;
			}
		}
		return true;
	}

	public static boolean dateVerify(int yyyy, int MM, int dd) {
		boolean flag = false;
		if ((yyyy > 1000) && (yyyy < 9999) && (MM >= 1) && (MM <= 12)
				&& (dd >= 1) && (dd <= 31)) {
			if ((MM == 4) || (MM == 6) || (MM == 9) || (MM == 11)) {
				if (dd <= 30)
					flag = true;
			} else if (MM == 2) {
				if (((yyyy % 100 != 0) && (yyyy % 4 == 0)) || (yyyy % 400 == 0)) {
					if (dd <= 29)
						flag = true;
				} else if (dd <= 28)
					flag = true;
			} else
				flag = true;
		}
		return flag;
	}

	public static boolean datetimeVerify(String datetime) {
		String regexp = "^(((20[0-3][0-9]-(0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|(20[0-3][0-9]-(0[2469]|11)-(0[1-9]|[12][0-9]|30))) (20|21|22|23|[0-1][0-9]):[0-5][0-9]:[0-5][0-9])$";
		try {
			PatternCompiler compiler = new Perl5Compiler();

			PatternMatcher matcher = new Perl5Matcher();

			Pattern hardPattern = compiler.compile(regexp);
			return matcher.contains(datetime, hardPattern);
		} catch (MalformedPatternException e) {
		}
		return false;
	}

	public static boolean indentityCodeVerify(String identityCode) {
		if ((identityCode == null) || (identityCode.equalsIgnoreCase("")))
			return false;
		identityCode = identityCode.trim();
		if ((identityCode.length() != 18) && (identityCode.length() != 15))
			return false;
		if (identityCode.length() == 15) {
			try {
				int y = 1900 + new Integer(identityCode.substring(6, 8))
						.intValue();
				int m = new Integer(identityCode.substring(8, 10)).intValue();
				int d = new Integer(identityCode.substring(10, 12)).intValue();
				return dateVerify(y, m, d);
			} catch (Exception e) {
				return false;
			}
		}
		int[] w = new int[19];
		w[2] = 2;
		w[3] = 4;
		w[4] = 8;
		w[5] = 5;
		w[6] = 10;
		w[7] = 9;
		w[8] = 7;
		w[9] = 3;
		w[10] = 6;
		w[11] = 1;
		w[12] = 2;
		w[13] = 4;
		w[14] = 8;
		w[15] = 5;
		w[16] = 10;
		w[17] = 9;
		w[18] = 7;
		int i = 0;
		int sum = 0;
		for (i = 1; i <= 17; i++) {
			int temp = new Integer(identityCode.substring(i - 1, i)).intValue();
			sum += temp * w[(19 - i)];
		}
		sum %= 11;
		String a18;
		switch (sum) {
		case 0:
		case 1:
			a18 = (1 - sum) + "";
			break;
		case 2:
			a18 = "X";
			break;
		default:
			a18 = (12 - sum) + "";
		}
		a18 = a18.substring(0, 1);
		String id = identityCode.substring(17, 18);

		return a18.equalsIgnoreCase(id);
	}
}